var mapSize = [1000,700];


var Navgrid = function() {
	
	
	var resolution = 10;
	var grid = {};
	var self = this;
	var generateGrid = function() {
		for ( var x = 0; x < mapSize[0]/resolution; x++) {
			grid[x] = {};
			for ( var y = 0; y < mapSize[1]/resolution; y++) {
				grid[x][y] = {"type": "empty", "rect": [[x*resolution,y*resolution],
				                                        [(x+1)*resolution,y*resolution],
				                                        [x*resolution,(y+1)*resolution],
				                                        [(x+1)*resolution,(y+1)*resolution]
				                                        ]};
			}
		}
	};
		
	this.requestGrid = function(pos) {
		return grid[Math.floor(pos[0]/resolution)][Math.floor(pos[1]/resolution)];
	};
	
	this.registerObjectOnGrid = function(obj) {
		// Obj has obj.shape, which if a circle there is obj.radius obj.center
		// if a rectangle has obj.points, which is an array of the 4 corners.
		// TODO: Nadi
		// Each case should mark the 'type' property of all grid squares under the shape (even partially) with the objType;
		// that is: grid[x][y].type = objType
		// the only question is: what are x,y for all grid squares under the shape?
		// hint: you can get the grid square under point x,y with nav.requestGrid;
		
		var objType = obj.type;
		
		// int radius, center is array - [x,y]
		function registerCircle(radius, center) {
			
			self.requestGrid(center).type = objType;
		}
		
		// corners array - [[x1,y1],[x2,y2],[x3,y3],[x4,y4]]
		function registerRect(corners) {
			
		};
		
		switch (obj.shape) {
			case "circle":
				var radius = obj.radius;
				var center = obj.center; // array - [x,y]
				registerCircle(radius, center);
				break;
			case "rect":
				var points = obj.points; // array - [[x1,y1],[x2,y2],[x3,y3],[x4,y4]]
				registerRect(corners);
				break;
			default:
				break;
		}
		return grid;
	}
	
	this.pathfind = function(spos, epos) {
		var snode = self.requestGrid(spos);
		var enode = self.requestGrid(epos);
		
		

	}
	
	generateGrid();
};


var nav = new Navgrid();

self.addEventListener('message', function(e) {
	//if(e.data.command=="requestGrid") {
		var response = nav[e.data.command].apply(nav,[e.data.details]);
		response.hash = e.data.hash;
		self.postMessage(response);

	//}
});
//postMessage(x);